package viettravel.guiandevent.map;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

public class GooglemapV2Activity extends SherlockFragmentActivity{
	private Double lat;
	private Double lng;
	private GoogleMap map;
	private boolean flag = true;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setTheme(Constants.THEME);
	    setContentView(R.layout.gmap_api_v2);
	    ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setIcon(R.drawable.ic_back_arrow);	
		actionBar.setBackgroundDrawable(Constants.APP_DRAWABLE);
		actionBar.setTitle(R.string.mapactivity_title);
		SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMapAPIMap);

		Log.v(Constants.TAG,"LOG 1");
		// Getting GoogleMap object from the fragment
		map = fm.getMap();
		// Enabling MyLocation Layer of Google Map
		map.setMyLocationEnabled(true);
		map.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
			@Override
			public void onMyLocationChange(Location location) {
				// TODO Auto-generated method stub
				lat = location.getLatitude();
				lng = location.getLongitude();
				if(flag && lat!= null && lng!=null){
					map.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)));
					map.animateCamera(CameraUpdateFactory.zoomTo(14), 3000, null);
					flag = false;
				}
			}
		});		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home: {
			finish();
			return true;
		}
		case R.id.mapactivity_submenuitem_getdirection: {
			Log.v(Constants.TAG, "get direction");
			Intent intent = new Intent(GooglemapV2Activity.this,
					GetDirectionDialog.class);
			intent.putExtra(Constants.MY_LOCATION,
					lat.toString() + "," + lng.toString());
			startActivity(intent);
			return true;
		}
		case R.id.mapactivity_submenuitem_placearound: {
			Log.v(Constants.TAG, "place around");
			Intent intent = new Intent(GooglemapV2Activity.this,
					CategoryDialog.class);
			intent.putExtra(Constants.MY_LOCATION,
					lat.toString() + "," + lng.toString());
			startActivity(intent);
			return true;
		}
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu _menu) {
		SubMenu subMenuSort = _menu.addSubMenu(this
				.getString(R.string.mapactivity_submenu_header));
		subMenuSort.add(Menu.NONE, R.id.mapactivity_submenuitem_getdirection,
				Menu.NONE, this.getString(R.string.mapactivity_getdirection))
				.setIcon(R.drawable.ic_get_direction);
		subMenuSort.add(Menu.NONE, R.id.mapactivity_submenuitem_placearound,
				Menu.NONE, this.getString(R.string.mapactivity_placearound))
				.setIcon(R.drawable.ic_place_around);
		MenuItem menuItemSort = subMenuSort.getItem();
		menuItemSort.setIcon(R.drawable.ic_list_w);
		menuItemSort.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS
				| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		return super.onCreateOptionsMenu(_menu);
	}
}
